package com.ruoyi.web.controller.client;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.client.*;
import com.ruoyi.system.service.IMesClientAllService;
import com.ruoyi.system.service.IMesClientConfigService;
import com.ruoyi.system.service.IMesClientPagesRelationService;
import com.ruoyi.system.service.IMesClientUsagesService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户端页面对应关系Controller
 *
 * @author ruoyi
 * @date 2023-02-20
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/client/mes_client_pages_relation" )
public class MesClientPagesRelationController extends BaseController {

    private final IMesClientPagesRelationService iMesClientPagesRelationService;
    private final IMesClientConfigService iMesClientConfigService;
    private final IMesClientAllService iMesClientAllService;
    private final IMesClientUsagesService iMesClientUsagesService;
    /**
     * 查询客户端页面对应关系列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MesClientPagesRelation mesClientPagesRelation)
    {
        startPage();
        LambdaQueryWrapper<MesClientPagesRelation> lqw = new LambdaQueryWrapper<MesClientPagesRelation>();
        if (mesClientPagesRelation.getClientId() != null){
            lqw.eq(MesClientPagesRelation::getClientId ,mesClientPagesRelation.getClientId());
        }
        if (mesClientPagesRelation.getPageId() != null){
            lqw.eq(MesClientPagesRelation::getPageId ,mesClientPagesRelation.getPageId());
        }
        if (StringUtils.isNotBlank(mesClientPagesRelation.getStatus())){
            lqw.eq(MesClientPagesRelation::getStatus ,mesClientPagesRelation.getStatus());
        }
        List<MesClientPagesRelation> list = iMesClientPagesRelationService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 根据客户端管理id查询 客户端使用处与页面分配的列表数据
     */
    @ApiOperation(value = "根据客户端管理id查询 客户端使用处与页面分配的列表数据")
    @GetMapping("/getAllInfo/{clientId}")
    public MesClientAllDTO getAllInfo(@PathVariable Integer clientId)
    {
        MesClientAllDTO info = iMesClientPagesRelationService.getAllInfo(clientId);
        return info;
    }

    /**
     * 导出客户端页面对应关系列表
     */
    @Log(title = "客户端页面对应关系" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(MesClientPagesRelation mesClientPagesRelation) {
        LambdaQueryWrapper<MesClientPagesRelation> lqw = new LambdaQueryWrapper<MesClientPagesRelation>(mesClientPagesRelation);
        List<MesClientPagesRelation> list = iMesClientPagesRelationService.list(lqw);
        ExcelUtil<MesClientPagesRelation> util = new ExcelUtil<MesClientPagesRelation>(MesClientPagesRelation. class);
        return util.exportExcel(list, "mes_client_pages_relation" );
    }

    /**
     * 获取客户端页面对应关系详细信息
     */
    @GetMapping(value = "/{relationId}" )
    public AjaxResult getInfo(@PathVariable("relationId" ) Long relationId) {
        return AjaxResult.success(iMesClientPagesRelationService.getById(relationId));
    }

    /**
     * 新增客户端页面对应关系
     */
    @Log(title = "客户端页面对应关系" , businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增客户端使用处")
    @PostMapping
    public AjaxResult add(@RequestBody MesClientPagesRelation mesClientPagesRelation) {

        return toAjax(iMesClientPagesRelationService.save(mesClientPagesRelation)? 1 : 0);
//        if((iMesClientPagesRelationService.save(mesClientPagesRelation) ? 1 : 0)>0){
//            Long villageId = mesClientPagesRelation.getRelationId();
//            LambdaQueryWrapper<MesClientUsages> lqw = new LambdaQueryWrapper<MesClientUsages>();
//            if (mesClientPagesRelation.getClientId() != null){
//                lqw.eq(MesClientUsages::getClientId ,mesClientPagesRelation.getClientId());
//            }
//            List<MesClientUsages> useUagessList = iMesClientUsagesService.list(lqw);
//            if (useUagessList.stream().count() > 0) {
//                MesClientAll clientEntity = iMesClientAllService.getById(mesClientPagesRelation.getClientId());
//                List<MesClientConfig> list = new ArrayList<>();
//                for (MesClientUsages item : useUagessList) {
//                    {
//                        MesClientConfig mesClientConfig = new MesClientConfig()
//                                .setPageId(mesClientPagesRelation.getPageId())
//                                .setStatus(item.getStatus())
//                                .setUsageId(item.getUsageId())
//                                .setPageNickName(clientEntity.getClientName())
//                                .setRelationId(villageId);
//                        list.add(mesClientConfig);
//                    }
//                    if (list.stream().count() > 0) {
//                        return toAjax((iMesClientConfigService.saveBatch(list) ? 1 : 0));
//                    }
//                    else {
//                        return toAjax(0);
//                    }
//                }
//            }
//            else {
//                return toAjax(0);
//            }
//            return toAjax(0);
//        }else {
//            return toAjax(0);
//        }
    }

    /**
     * 修改客户端页面对应关系
     */
    @Log(title = "客户端页面对应关系" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesClientPagesRelation mesClientPagesRelation) {
        return toAjax(iMesClientPagesRelationService.updateById(mesClientPagesRelation) ? 1 : 0);
    }

    /**
     * 删除客户端页面对应关系
     */
    @Log(title = "客户端页面对应关系" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{relationIds}" )
    public AjaxResult remove(@PathVariable Long[] relationIds) {
        return toAjax(iMesClientPagesRelationService.removeByIds(Arrays.asList(relationIds)) ? 1 : 0);
    }
}
